<template>
  <div>
    <el-form :model="searchForm" inline>
      <el-form-item label="图书名称">
        <el-input placeholder="图书名称" clearable v-model="searchForm.bookName"/>
      </el-form-item>
      <el-form-item label="作者名称">
        <el-input placeholder="作者名称" clearable v-model="searchForm.authorName"/>
      </el-form-item>
      <el-form-item label="图书状态">
        <el-select placeholder="选择图书状态" clearable v-model="searchForm.bookStatus">
          <el-option label="连载中" :value="1"/>
          <el-option label="已完结" :value="2"/>
        </el-select>
      </el-form-item>
      <el-form-item label="信息状态">
        <el-select placeholder="选择信息状态" clearable v-model="searchForm.status">
          <el-option label="可用" :value="1"/>
          <el-option label="禁用" :value="2"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getDataList">查询</el-button>
        <el-button @click="openDialog()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border>
      <el-table-column prop="bookId" label="编号" align="center"/>
      <el-table-column prop="bookName" show-overflow-tooltip label="图书名" align="center"/>
      <el-table-column prop="authorName" label="作者名" align="center"/>
      <el-table-column prop="profile" label="图书简介" show-overflow-tooltip align="center"/>
      <el-table-column label="图书评分" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.scoreCount >0">{{(scope.row.score/scope.row.scoreCount).toFixed(2)}}</span>
          <span v-else>暂无评分</span>
        </template>
      </el-table-column>
      <el-table-column prop="cover" label="图书封面" align="center" width="230px">
        <template slot-scope="scope">
          <el-image @click.native="showImageDialog(scope.row.cover)"
            style="width: 200px; height: 100px" :src="baseImageUrl+scope.row.cover" fit="cover"></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="bookStatus" label="图书状态" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.bookStatus === 1">连载中</el-tag>
          <el-tag type="success" v-else>已完结</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" align="center">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.status === 1">可用</el-tag>
          <el-tag type="danger" v-else>禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="目录" fixed="right">
        <template slot-scope="scope">
        <el-button size="small" @click="toCatalog(scope.row.bookId)">目录</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="160px">
        <template slot-scope="scope">
          <el-button size="small" type="primary" @click="openDialog(scope.row.bookId)">修改</el-button>
          <el-button size="small" type="warning" @click="deleteInfo(scope.row.bookId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination hide-on-single-page :current-page="pageInfo.page" :page-size="pageInfo.limit" :total="pageInfo.total" :page-sizes="[5,10,20,50,100]" layout="total,sizes,prev,pager,next,jumper" @current-change="currentChange" @size-change="sizeChange"/>
    <BookUpdate @onCloseDialog="getDataList" ref="bookUpdate"/>
    <el-dialog title="图像详情" :visible.sync="showImage" v-if="showImage" custom-class="img-center">
      <div style="height: 100%;width: 100%;display: flex;align-items: center;justify-content: center;">
        <img :src="showImageUrl" alt="" style="display: block;max-height: 100%;max-width: 100%">
      </div>
    </el-dialog>
  </div>
</template>

<script>
import BookUpdate from '@/views/admin/book/BookUpdate.vue'
export default {
  name: 'BookList',
  components: { BookUpdate },
  data () {
    return {
      searchForm: {
        status: '',
        bookStatus: '',
        bookName: '',
        authorName: ''
      },
      pageInfo: {
        page: 1,
        limit: 5,
        total: 0
      },
      dataList: [],
      baseImageUrl: process.env.VUE_APP_BASE_URL + '/img/',
      showImage: false,
      showImageUrl: ''
    }
  },
  methods: {
    openDialog (bookId) {
      this.$refs.bookUpdate.init(bookId)
    },
    deleteInfo (bookId) {
      this.$confirm(`确定删除编号为[${bookId}]的数据吗？`).then(() => {
        this.$http.delete(`book/delete/${bookId}`).then(() => {
          this.$message.success('删除成功！')
          this.getDataList()
        })
      }).catch(() => {})
    },
    currentChange (val) {
      this.pageInfo.page = val
      this.getDataList()
    },
    sizeChange (val) {
      this.pageInfo.limit = val
      this.getDataList()
    },
    getDataList () {
      const params = {
        ...this.pageInfo,
        ...this.searchForm
      }
      this.$http.get('book/page', params).then(({ data }) => {
        this.dataList = data.records
        this.pageInfo.page = data.current
        this.pageInfo.total = data.total
      })
    },
    showImageDialog (src) {
      this.showImageUrl = this.baseImageUrl + src
      this.showImage = true
    },
    toCatalog (bookId) {
      this.$router.push(`catalog?bookId=${bookId}`)
    }
  },
  created () {
    this.$nextTick(() => {
      this.getDataList()
    })
  }
}
</script>

<style scoped>

</style>
